Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-custom-error

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-custom-error

Extend native Error to create custom errors

  • 3.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
148K
decreased by-61.65%
Maintainers
1
Weekly downloads
 
Created

What is ts-custom-error?

The ts-custom-error package is a TypeScript library that simplifies the creation of custom error classes. It provides a base class that can be extended to create custom error types with minimal boilerplate code.

What are ts-custom-error's main functionalities?

Creating Custom Error Classes

This feature allows you to create custom error classes by extending the CustomError base class provided by ts-custom-error. The example demonstrates how to create a custom error class named MyCustomError and throw an instance of it.

class MyCustomError extends CustomError {
  constructor(message: string) {
    super(message);
    this.name = 'MyCustomError';
  }
}

try {
  throw new MyCustomError('Something went wrong!');
} catch (error) {
  console.error(error.name); // MyCustomError
  console.error(error.message); // Something went wrong!
}

Preserving Stack Trace

This feature ensures that the stack trace is preserved when custom errors are thrown. The example shows how the stack trace includes the function where the error was thrown, which aids in debugging.

class AnotherCustomError extends CustomError {
  constructor(message: string) {
    super(message);
    this.name = 'AnotherCustomError';
  }
}

function problematicFunction() {
  throw new AnotherCustomError('An error occurred in problematicFunction');
}

try {
  problematicFunction();
} catch (error) {
  console.error(error.stack); // Stack trace includes problematicFunction
}

Other packages similar to ts-custom-error

Keywords

FAQs

Package last updated on 01 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc